$(function() {

	//----------------------banner轮播
	//初始化  把所有的li的透明度改为0 然后显示第一个li元素
	$(".banner .img li").css({
		opacity: 0
	}).eq(0).css({
		opacity: 1
	})

	//初始化显示第一个按钮状态
	$(".banner .btn li").eq(0).css({
		background: "red"
	})

	function show(cur) {
		//显示一张图片
		$(".banner .img li").eq(cur).animate({
			opacity: 1
		}, 1000)

		//隐藏一张图片
		$(".banner .img li").eq(cur - 1).animate({
			opacity: 0
		}, 1000)

		//显示按钮状态
		$(".banner .btn li").eq(cur).css({
			background: "red"
		}).siblings().css({
			background: "#757175"
		})
	}

	//单图动画
	function ani() {
		cur++;
		if(cur == $(".banner .img li").size()) {
			cur = 0
		}
		show(cur);
	}
	var cur = 0;
	var timer = setInterval(ani, 3000);

	//鼠标悬停按钮上 显示相应的图片
	$(".banner .btn li").on("mouseenter", function() {
		btn_show($(this).index());
		cur = $(this).index();
	})

	//显示当前悬停按钮所对应的图片

	function btn_show(cur) {

		//显示一张图片
		$(".banner .img li").eq(cur).animate({
			opacity: 1
		}).siblings().animate({
			opacity: 0
		})

		//显示按钮状态
		$(".banner .btn li").eq(cur).css({
			background: "red"
		}).siblings().css({
			background: "#757175"
		})

	}

	//单击往左a标签
	$(".banner .control_wrapper .to_left").on("click", function() {
		if($(".banner .img li").is(":animated")) {
			return;
		}

		cur -= 1;
		if(cur == -1) {
			cur = $(".banner .img li").size() - 1;
		}
		btn_show(cur);
	})

	//单击往右a标签
	$(".banner .control_wrapper .to_right").on("click", function() {
		if($(".banner .img li").is(":animated")) {
			return;
		}

		cur += 1;
		if(cur == $(".banner .img li").size()) {
			cur = 0;
		}
		btn_show(cur);
	})

	//鼠标悬停在图片上停止定时器   离开时启动定时器
	$(".banner").hover(
		function() {
			clearInterval(timer);
		},
		function() {
			timer = setInterval(ani, 3000);
		}
	);

	//----------------------侧边栏
	//返回顶部初始化隐藏
	$(".sidebarIcon .bottom .li2").css({
		display: "none"
	})

	//窗口滚动时 显示/隐藏返回按钮
	$(window).scroll(function() {
		if($(document).scrollTop() >= $(".header").offset().top) {
			$(".sidebarIcon .bottom .li2").css({
				display: "block"
			})
		} else {
			$(".sidebarIcon .bottom .li2").css({
				display: "none"
			})
		}
	})

	//返回顶部
	$(".sidebarIcon .bottom .li2").on("click", function() {
		$("body").animate({
			scrollTop: 0
		}, 200)
	})

	//-----------------从cookie里获得用户名
	function getCookies() {
		var Cookie_name = "";
		var Cookie_value = "";
		if(!document.cookie == '') {
			var arrCookie = document.cookie.split('; ');
			//用spilt('; ')切割所有cookie保存在数组arrCookie中 
			for(var i = 0; i < arrCookie.length; i++) {
				if(unescape(arrCookie[i].split('=')[0]) == "huausername") {
					Cookie_name = unescape(arrCookie[i].split('=')[0]);
					Cookie_value = unescape(arrCookie[i].split('=')[1]);
					break;
				}
			}
			if(Cookie_value == "") {
				Cookie_value = "请登录"
			}

			return Cookie_value;
		} else {
			return Cookie_value = "请登录"
		}
	}

	//设置用户名
	var username = getCookies();

	if(username != "请登录") {
		var newusername = username.substring(0, username.indexOf("@"));
		$(".site_nav .center_ul_r .li1 span").html(newusername);
	}

	//显示或者隐藏元素
	//如果用户已经登录就显示退出按钮，同时隐藏注册按钮
	if(username != "请登录") {
		$(".site_nav .center_ul_r .li2 .register").css({
			display: "none"
		});
	} else {
		$(".site_nav .center_ul_r .li2 .out").css({
			display: "none"
		});
	}

	//点击退出 清除cookie
	$(".site_nav .center_ul_r .li2 .out").on("click", function() {
		document.cookie = "huausername=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
		window.location.href = "index.php";
	})

	//控制购物车页面的跳转
	$(".site_nav .center_ul_r .li6>a").on("click", function() {
		var count = $(".sidebarIcon .li2 .cart_num").html();
		if(count > 0) {
			window.location.href = "ShowCart.php";
		} else {
			window.location.href = "ShopNothing.html";
		}

	})

	$(".sidebarIcon .li2").eq(0).on("click", function() {
		var count = $(".sidebarIcon .li2 .cart_num").html();
		if(count > 0) {
			window.location.href = "ShowCart.php";
		} else {
			window.location.href = "ShopNothing.html";
		}
	})

})